package com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.logreverse.ExtractHelper;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueryBlackOweBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(QueryBlackOweBizSlot.class);
    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String staffId = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/STAFF_ID");
            String certificateType = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/CERTIFICATE_TYPE");
            String certificateNo = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/CERTIFICATE_NO");
            String channelCode = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/CHANNEL_CODE");



            //构建请求条件
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("staffId", staffId);
            jsonObject.put("certificateType", certificateType);
            jsonObject.put("certificateNo", certificateNo);
            jsonObject.put("channelCode", channelCode);
            StringBuilder msgBodyStr = new StringBuilder();

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            msgBodyStr.append(jsonObject.toJSONString());

            _log.info("request body is {}", msgBodyStr.toString());
            _log.info("webserviceUrl is {}", webserviceUrl);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), webserviceUrl);
            _log.info("response is {}", respStr);

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            //解析json 拼接报文
            JSONObject json = JSON.parseObject(respStr);
            StringBuffer buffer = new StringBuffer();
            jsonToXmlStr(xmlMessage,json, buffer);


            bizResult.setHttpBody(buffer.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(XmlMessage xmlMessage, JSONObject json, StringBuffer buffer) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
        buffer.append("<soap:Body xmlns:ns1=\"http://www.chinatelecom.cn/SGW/SOAP\">");
        buffer.append("<ns1:SgwQueryAck xmlns=\"\">");
        buffer.append("<MID>" + xmlMessage.getMID() + "</MID>");
        buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
        buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
        buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
        buffer.append("<DATA><![CDATA[<Service_Information>");

        buffer.append("<Result>" + (json.get("resultCode") == null ? "" : json.get("resultCode")) + "</Result>");
        buffer.append("<RESULT>" + (json.get("resultCode") == null ? "" : json.get("resultCode")) + "</RESULT>");
        buffer.append("<ERR_INFO>" + (json.get("resultMsg") == null ? "" : json.get("resultMsg")) + "</ERR_INFO>");



        String entryListStr = String.valueOf(json.get("oweItem"));
        JSONArray jsonArray = JSONArray.parseArray(entryListStr);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject entry = (JSONObject) jsonArray.get(i);
            buffer.append("<OWE_ITEM>");
            buffer.append("<CERTIFICATE_TYPE>" + (entry.get("certificateType") == null ? "" : json.get("certificateType")) + "</CERTIFICATE_TYPE>");
            buffer.append("<CERTIFICATE_NO>" + (entry.get("certificateNo") == null ? "" : entry.get("certificateNo")) + "</CERTIFICATE_NO>");
            buffer.append("<ACCT_NAME>" + (entry.get("acctName") == null ? "" : entry.get("acctName")) + "</ACCT_NAME>");
            buffer.append("<ACC_NBR>" + (entry.get("accNbr") == null ? "" : entry.get("accNbr")) + "</ACC_NBR>");
            buffer.append("<SERV_ID>" + (entry.get("servId") == null ? "" : entry.get("servId")) + "</SERV_ID>");
            buffer.append("<REGION_ID>" + (entry.get("regionId") == null ? "" : entry.get("regionId")) + "</REGION_ID>");
            buffer.append("<SERV_TOTAL_OWE>" + (entry.get("servTotalOwe") == null ? "" : entry.get("servTotalOwe")) + "</SERV_TOTAL_OWE>");
            buffer.append("<ACCT_ID>" + (entry.get("acctId") == null ? "" : entry.get("acctId")) + "</ACCT_ID>");
            buffer.append("<ACCT_TOTAL_OWE>" + (entry.get("acctTotalOwe") == null ? "" : entry.get("acctTotalOwe")) + "</ACCT_TOTAL_OWE>");
            buffer.append("<ACCT_BAD_OWE>" + (entry.get("acctBadOwe") == null ? "" : entry.get("acctBadOwe")) + "</ACCT_BAD_OWE>");
            buffer.append("<ONCE_OWE>" + (entry.get("onceOwe") == null ? "" : entry.get("onceOwe")) + "</ONCE_OWE>");
            buffer.append("<ITEM_OWE>" + (entry.get("itemOwe") == null ? "" : entry.get("itemOwe")) + "</ITEM_OWE>");
            buffer.append("<CAN_RECHARGE>" + (entry.get("canRecharge") == null ? "" : entry.get("canRecharge")) + "</CAN_RECHARGE>");
            buffer.append("</OWE_ITEM>");
        }


        buffer.append("</Service_Information>]]></DATA>");
        buffer.append("</ns3:SgwQueryAck>");
        buffer.append("</SOAP-ENV:Body>");
        buffer.append("</SOAP-ENV:Envelope>");
    }

}
